package com.example.professorevaluation.dao

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.professorevaluation.bean.ProfessorBean
import com.example.professorevaluation.bean.RateBean

@Database(entities = [ProfessorBean::class, RateBean::class], version = 1, exportSchema = false)
abstract class ProfessorDatabase: RoomDatabase() {
    abstract fun teacherDao(): ProfessorDao
    abstract fun evaluationDao(): RateDao
    companion object{
        @Volatile private var instance:ProfessorDatabase? = null
        fun getInstance(context: Context):ProfessorDatabase{
            return instance?: synchronized(this) {
                instance?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context):ProfessorDatabase{
            return Room.databaseBuilder(context,ProfessorDatabase::class.java, "db_professor")
                .allowMainThreadQueries()
                .addCallback(object : Callback() {
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                    }
                })
                .fallbackToDestructiveMigration()
                .build()

        }
    }
}